/*
 * Component.cpp
 */

#include "Component.h"
#include "ComponentManager.h"
#include "GraphicContext.h"

namespace gui
  {
    int Component::_lastId = 0;
    GraphicContext Component::_maskGc;

    Component::Component()
      {
        _parent = NULL;
      }

    int const& Component::id() const { return _id; }

    void Component::init()
      {
        _id = _lastId = _lastId + 100;
        _position.x = 0;
        _position.y = 0;
        _size.width = 100;
        _size.height = 100;
      }

    void Component::process() {}

    Component* Component::parent(Component* parent) { return _parent = parent; }
    Component* Component::parent() { return _parent; };

    GraphicContext const& Component::maskGc() const { return _maskGc; }

    bool const Component::hasFocus() const
      {
        Component *comp = ComponentManager::instance()->focused();
        return (comp != NULL) && (comp->id() == id());
      }

    void Component::mask(BITMAP* buffer, int x_offset, int y_offset)
      {
        maskGc().activate();
        int x1 = x_offset + _position.x;
        int y1 = y_offset + _position.y;
        int x2 = x1 + _size.width;
        int y2 = y1 + _size.height;
        rectfill(buffer, x1, y1, x2, y2, _id); 
      }

    int const& Component::x() const { return _position.x; }
    int const& Component::y() const { return _position.y; }
    int const& Component::width() const { return _size.width; }
    int const& Component::height() const { return _size.height; }

    Point const& Component::position() const { return _position; }

    Point const& Component::move(Point& point) 
      { 
        _position.x = point.x;
        _position.y = point.y;
        return _position; 
      }

    Point const& Component::move(int x, int y)
      { 
        _position.x = x;
        _position.y = y;
        return _position; 
      }

    Size const& Component::size() const { return _size; }

    Size const& Component::size(Size& size) 
      { 
        _size.width = size.width;
        _size.height = size.height;
        return _size; 
      }

    Size const& Component::size(int width, int height)
      { 
        _size.width = width;
        _size.height = height;
        return _size; 
      }

  }

